package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;

import com.photoeditor.R;


/**
 * 可选中的ImageView
 */
public class CheckableImageView extends AppCompatImageView implements Checkable {

    private boolean mChecked = false;

    private Drawable mDrawable;

    private Drawable mCheckedDrawable;

    private Drawable mBgDrawable;

    private Drawable mBgCheckedDrawable;

    private OnClickListener mListener;

    public CheckableImageView(Context context) {
        this(context, null);
    }

    public CheckableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWithAttr(attrs);
        initView();
    }

    private void initWithAttr(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CheckableImageView);
            int srcId = typedArray.getResourceId(R.styleable.CheckableImageView_src, View.NO_ID);
            int srcSelectedId = typedArray.getResourceId(R.styleable.CheckableImageView_srcSelected, View.NO_ID);
            int bgSrcId = typedArray.getResourceId(R.styleable.CheckableImageView_bgSrc, View.NO_ID);
            int bgSrcSelectedId = typedArray.getResourceId(R.styleable.CheckableImageView_bgsrcSelected, View.NO_ID);
            if (srcId != View.NO_ID) {
                mDrawable = getResources().getDrawable(srcId);
            }
            if (srcSelectedId != View.NO_ID) {
                mCheckedDrawable = getResources().getDrawable(srcSelectedId);
            }
            if (bgSrcId == View.NO_ID) {
                int color = typedArray.getColor(R.styleable.CheckableImageView_bgSrc, Color.TRANSPARENT);
                if (color != Color.TRANSPARENT) {
                    mBgDrawable = new ColorDrawable(color);
                }
            } else {
                mBgDrawable = getResources().getDrawable(bgSrcId);
            }

            if (bgSrcSelectedId == View.NO_ID) {
                int color = typedArray.getColor(R.styleable.CheckableImageView_bgsrcSelected, Color.TRANSPARENT);
                if (color != Color.TRANSPARENT) {
                    mBgCheckedDrawable = new ColorDrawable(color);
                }
            } else {
                mBgCheckedDrawable = getResources().getDrawable(bgSrcSelectedId);
            }

            mChecked = typedArray.getBoolean(R.styleable.CheckableImageView_selected, false);
            if (mChecked) {
                if (mBgCheckedDrawable != null) {
                    setBackgroundDrawable(mBgCheckedDrawable);
                }
                if (mCheckedDrawable != null) {
                    setImageDrawable(mCheckedDrawable);
                }
            } else {
                if (mBgDrawable != null) {
                    setBackgroundDrawable(mBgDrawable);
                }
                if (mDrawable != null) {
                    setImageDrawable(mDrawable);
                }
            }
            typedArray.recycle();
        }

    }

    private void initView() {
    }

    /**
     * 设置显示Drawable
     *
     * @param drawable
     * @param checkedDrawable
     */
    public void setImageDrawable(Drawable drawable, Drawable checkedDrawable) {
        mDrawable = drawable;
        mCheckedDrawable = checkedDrawable;
        if (mChecked) {
            if (mBgCheckedDrawable != null) {
                setBackgroundDrawable(mBgCheckedDrawable);
            }
            if (mCheckedDrawable != null) {
                setImageDrawable(mCheckedDrawable);
            }
        } else {
            if (mBgDrawable != null) {
                setBackgroundDrawable(mBgDrawable);
            }
            if (mDrawable != null) {
                setImageDrawable(mDrawable);
            }
        }
    }

    /**
     * 设置背景的Drawable
     *
     * @param bgDrawable
     * @param bgCheckedDrawable
     */
    public void setBackgroundDrawable(Drawable bgDrawable, Drawable bgCheckedDrawable) {
        mBgDrawable = bgDrawable;
        mBgCheckedDrawable = bgCheckedDrawable;
        if (mChecked) {
            if (mBgCheckedDrawable != null) {
                setBackgroundDrawable(mBgCheckedDrawable);
            }
            if (mCheckedDrawable != null) {
                setImageDrawable(mCheckedDrawable);
            }
        } else {
            if (mBgDrawable != null) {
                setBackgroundDrawable(mBgDrawable);
            }
            if (mDrawable != null) {
                setImageDrawable(mDrawable);
            }
        }
    }

    /**
     * @param bgColor
     * @param bgCheckedColor
     */
    public void setBackgroundColor(int bgColor, int bgCheckedColor) {
        mBgDrawable = new ColorDrawable(bgColor);
        mBgCheckedDrawable = new ColorDrawable(bgCheckedColor);
        if (mChecked) {
            if (mBgCheckedDrawable != null) {
                setBackgroundDrawable(mBgCheckedDrawable);
            }
            if (mCheckedDrawable != null) {
                setImageDrawable(mCheckedDrawable);
            }
        } else {
            if (mBgDrawable != null) {
                setBackgroundDrawable(mBgDrawable);
            }
            if (mDrawable != null) {
                setImageDrawable(mDrawable);
            }
        }
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            if (mChecked) {
                if (mBgCheckedDrawable != null) {
                    setBackgroundDrawable(mBgCheckedDrawable);
                }
                if (mCheckedDrawable != null) {
                    setImageDrawable(mCheckedDrawable);
                }
            } else {
                if (mBgDrawable != null) {
                    setBackgroundDrawable(mBgDrawable);
                }
                if (mDrawable != null) {
                    setImageDrawable(mDrawable);
                }
            }
        }
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        mListener = l;
        super.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setChecked(true);
                if (mListener != null) {
                    mListener.onClick(v);
                }
            }
        });
    }
}
